<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";
require "submenu.php";

$label = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));

if ($submit)
{

	$startdate = $fyear."-".$fmonth."-".$fday." 00:00:00";
	$enddate = $tyear."-".$tmonth."-".$tday." 00:00:00";

	//check variables
	if (strlen($adcode) > 100)
		error("adcode must be less than 100 characters.");
	elseif (!$adcode)
		error("ad code is a required field.");
	elseif (preg_match("/(\W+|\s+)/", $adcode, $matches))
		error("Ad code must contain letters,digits or and underscore character only.");
	elseif (strlen($url) > 255)
		error("Url must be less than 255 characters.");
	elseif (!$url)
		error("Url is a required field.");
	elseif (!preg_match("/(http|https):\/\//", $url))
		error("Url is not valid.");
	elseif (!preg_match("/\b\d+\b/", $cost))
		error("Cost must be in numerical format.");
	elseif (earlier_date($startdate,$enddate) == $enddate)
		error("Start date must be earlier than end date.");
	elseif ($recur && $ppc)
		error("Please select recurring or ppc. Both can't be selected at the same time.");
	elseif ($recur && (!$recurdays || !$repeat))
		error("Recurring cost and repeating cost must be filled out if recurring is selected.");
	elseif ($recurdays && !preg_match("/\b\d+\b/", $recurdays))
		error("Recurring days must be an integer value.");
	elseif ($repeat && !preg_match("/\b\d+\b/", $repeat))
		error("Repeat must be an integer value.");
	else
	{
		?>
		<table width="100%">
		<tr>
			<td>
				<p class="small" align="left">Campaigns > <?=$label[0]?> > Edit</p>
			</td>
			<td>
			
				<? 
					echo "$label[0]";
					load_submenu($cID,$label[0]);
						?>
			</td>
		</tr>
		</table>

		<p class="heading">
		Edit an Existing Campaign
		</p>
		<p>
		To make changes to your campaign edit the fields below and click save changes.
		</p>
		<?

		if ($ppc)
			$ppc = 1;
		
		if ($group == "default")
			$groupID = "0";
		else
			$groupID = mysql_fetch_row(mysql_query("select ID from adtrackz_groups where ID='$group'"));

		$adcode = trim($adcode);
		$adcode = strtolower($adcode);		

		$old_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));
		if ($old_adcode[0] != $adcode)
			$msg = "<p><b>Warning:</b>You have changed the adcode for this campaign. Consequently, any redirect files you have created for this campaign will no longer work. To fix this problem, you can delete all redirect files associated with this campaign and recreate them again, the new adcode will be used. Or you can simply change the adcode back to what it was previously.";

		mysql_query("update adtrackz_campaigns set groupID='$groupID[0]', adcode='$adcode', url='$url', cost='$cost', startdate='$startdate', enddate='$enddate', description='$description', purpose='$purpose', recurdays='$recurdays', repeat='$repeat', ppc='$ppc' where ID='$cID'");

		mysql_query("update adtrackz_campaigns_st set st='$st' where campaignID='$cID'");

		//mysql_query("update campaigns_optional set contact='$optcontact', phone='$optphone', email='$optemail', url='$opturl', notes='$optnotes' where campaignID='$cID'");
		//echo mysql_error();

		alert ("$adcode has been updated successfully.$msg");
	}
}
else //didn't click submit button to save changes, but may have chosen a campaign to edit, reset or del
{
	if (!$cID) //no campaign ID, display campaign selector
	{
		?>
		<p class="heading">
		Edit an Existing Campaign
		</p>
		<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
		<tr>
			<td>Select a campaign.</td>
			<td>
				<form action="cpgn-edit.php" method="post">
				<select name="cID">
				<?
				$campaigndata = mysql_query("select * from adtrackz_campaigns order by adcode asc");
				while($campaign = mysql_fetch_row($campaigndata))
					echo "<option value=\"$campaign[0]\">$campaign[2]</option>";
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Select an action.</td>
			<td>
				<select name="action">
				<option value=edit>Edit</option>
				<option value=reset>Reset Hits</option>
				<option value=delete>Delete</option>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<br><br><input type="submit" value="Modify Campaign" name="selectcampaign"  >
			</td>
		</tr>
		</table>
		</form>
		<?
		require "footer.php";
		exit;
	}
	elseif($action == "delete") 
	{
		if (!$confirm)
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Delete</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Campaign Delete
			</p>

			<?
			$campaign = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));
			alert ("<p>Are you sure you want to remove <b>$campaign[0]</b>? All existing data for this campaign will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action=cpgn-edit.php method=post>
					<input type=submit name=submit_delete value=" Yes ">
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=hidden name=action value=delete>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action=cpgn-edit.php method=post>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Delete</p>
				</td>
				<td>
					
				</td>
			</tr>
			</table>

			<p class=heading>
			Campaign Delete
			</p>
			<?
			$campaign = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));
			//what if campaign is in a split test group
			if (mysql_fetch_row(mysql_query("select * from adtrackz_split_test_campaigns where campaignID='$cID'")))
				alert("$campaign[0] can't be deleted because it's part of a split test. Delete $campaign[0] from the split test and try again.");
			else
			{
				mysql_query("delete from adtrackz_campaigns where ID='$cID'");
				mysql_query("delete from adtrackz_clicks where campaignID='$cID'");
				mysql_query("delete from adtrackz_actions where campaignID='$cID'");
				mysql_query("delete from adtrackz_sales where campaignID='$cID'");
				mysql_query("delete from adtrackz_campaigns_st where campaignID='$cID'");
				alert("$campaign[0] has been deleted.");
			}
			require "footer.php";
			exit;
		}
	}
	elseif($action == "reset") 
	{
		$campaign = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));
		if (!$confirm)
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Reset</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Campaign Reset
			</p>
			<?
			alert ("<p>Are you sure you want to remove the clicks, actions and sales for the campaign: <b>$campaign[0]</b>? All existing data for this campaign will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action=cpgn-edit.php method=post>
					<input type=submit name=submit_delete value=" Yes ">
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=hidden name=action value=reset>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action=cpgn-edit.php method=post>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			?>
			<table width="100%">
			<tr>
				<td>
					<p class="small" align="left">Campaigns > <?=$label[0]?> > Reset</p>
				</td>
				<td>
					<?load_submenu($cID,$label[0])?>
				</td>
			</tr>
			</table>

			<p class=heading>
			Campaign Reset
			</p>
			<?
			mysql_query("delete from adtrackz_clicks where campaignID='$cID'");
			mysql_query("delete from adtrackz_actions where campaignID='$cID'");
			mysql_query("delete from adtrackz_sales where campaignID='$cID'");
			alert("All clicks, actions and sales have been deleted for $campaign[0].");
			require "footer.php";
			exit;
		}
	}
	else //there is a campaign ID, let's edit, set variables
	{
		$campaigndata = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));
		$campaign_st = mysql_fetch_row(mysql_query("select st from adtrackz_campaigns_st where campaignID='$cID'"));
		$adcode = $campaigndata[2];

	
		//purpose
		/*
		if ($campaigndata[8] == "all")
			$purpose = "Clicks, Actions & Sales";
		elseif ($campaigndata[8] == "clicks")
			$purpose = "Clicks Only";
		elseif ($campaigndata[8] == "actions")
			$purpose =  "Clicks & Actions";
		elseif ($campaigndata[8] == "sales")
			$purpose = "Clicks & Sales";
		elseif ($campaigndata[8] == "affiliate")
			$purpose = "Clicks, Sales, Actions on Affiliate Websites";
			*/
		$purpose = $campaigndata[8];

		//group
		if ($campaigndata[1])
		{
			$groupdata = mysql_fetch_row(mysql_query("select ID from adtrackz_groups where ID='$campaigndata[1]'"));
			$group = $groupdata[0];
		}

		$st = $campaign_st[0];
		$url = $campaigndata[3];
		$cost = $campaigndata[4];
		$cost = number_format($cost, 2);
		$ppc = $campaigndata[11];
		if (!$campaigndata[9])
			$recurdays = '';
		else
			$recurdays = $campaigndata[9];
		if (!$campaigndata[10])
			$repeat = '';
		else
			$repeat = $campaigndata[10];
		$description = $campaigndata[7];
		$tmp1 = explode(" ", $campaigndata[5]);
		list( $fyear,$fmonth, $fday) = explode("-",$tmp1[0]);
		$tmp2 = explode(" ", $campaigndata[6]);
		list($tyear, $tmonth, $tday) = explode("-",$tmp2[0]);
		$fmonth = trim(intval($fmonth));
		$fday = trim(intval($fday));
		$tmonth = trim(intval($tmonth));
		$tday = trim(intval($tday));
		
		?>
		<table width="100%">
		<tr>
			<td>
				<p class="small" align="left">Campaigns > <?=$label[0]?> > Edit</p>
			</td>
			<td>
				<?load_submenu($cID,$label[0])?>
			</td>
		</tr>
		</table>

		<p class="heading">
		Edit an Existing Campaign
		</p>
		<p>
		To make changes to your campaign edit the fields below and click save changes.
		</p>
		<?
	}
}


?>


<form action="cpgn-edit.php" method="post">
<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
<tr>
	<td valign="top">Ad Code</td>
	<td valign="top">
		<input type="text" size="20" name="adcode" value="<?=$adcode?>"> 		
	</td>
</tr>
<tr>
	<td valign="top">Url</td>
	<td valign="top">
		<input type="text" size="30" name="url" value="<?=$url?>">
	</td>
</tr> 
<tr>
	<td valign="top">Group</td>
	<td valign="top">			
			<select name="group">
			<?
			$groups = mysql_query("select * from adtrackz_groups order by groupname asc");
			if(!$group)
			{
				echo "<option value=\"default\" selected>- default -</option>";
				while($group1 = mysql_fetch_row($groups))
						echo "<option value=\"$group1[0]\">$group1[1]</option>";
			}
			else
			{
				echo "<option value=\"default\">- default -</option>";
				while($group1 = mysql_fetch_row($groups))
				{
					if ($group == $group1[0])
						echo "<option value=\"$group1[0]\" selected>$group1[1]</option>";
					else
						echo "<option value=\"$group1[0]\">$group1[1]</option>";
				}
			}
			?>
			</select>
	</td>
</tr>
<tr>
	<td valign="top">Purpose</td>
	<td valign="top">			
		<?
			if (!$purpose || $purpose == "clicks")
				$clicks_s = "selected";
			elseif ($purpose == "actions")
				$actions_s = "selected";
			elseif ($purpose == "sales")
				$sales_s = "selected";
			elseif ($purpose == "all")
				$all_s = "selected";
			elseif ($purpose == "affiliate")
				$affiliate_s = "selected";
		?>
		<select name="purpose">
		<option value="clicks" <?=$clicks_s?>>Clicks Only</option>
		<option value="actions" <?=$actions_s?>>Clicks & Actions</option>
		<option value="sales" <?=$sales_s?>>Clicks & Sales</option>
		<option value="all" <?=$all_s?>>Clicks, Actions & Sales</option>
		<option value="affiliate" <?=$affiliate_s?>>Affiliate Link</option>
		</select> 
	</td>
</tr>
<tr>
	<td valign="top">Split Test Stats Only</td>
	<td valign="top">			
		<?
			if ($st)
				$st = "checked";
		?>
		<input type=checkbox name=st <?=$st?>>
	</td>
</tr>
<tr>
	<td valign="top">Cost</td>
	<td valign="top">
		<?if (!$cost)
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="0.00"> 
		<?}
		else
		{?>
			<input type="text" size="6" maxlength="10" name="cost" value="<?=$cost?>"> 
		<?}?>
	</td>
</tr>
<tr>
	<td valign="top">Cost Type</td>
	<td valign="top">
		<?
		if ($ppc)
			$ppc_s = "checked";
		if ($recur)
			$recur_s = "checked";
		?>
		<input type="checkbox" name="ppc" <?=$ppc_s?>> PPC, or <br>
		<input type="checkbox" name="recur" <?=$recur_s?>> Recurring over <input type="text" size="2" maxlength="4"  name="recurdays" value=<?=$recurdays?>> days
		 repeating <input type="text" size="2" maxlength="4" name="repeat" value="<?=$repeat?>"> times.
	</td>
</tr> 
<tr>
	<td valign="top">Start Date</td>
	<td valign="top">
	<select name="fmonth">
<?
//set the month
if (!$fmonth)
	$now_month = date("n");
else
	$now_month = $fmonth;
$months = array(1 => 'Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$i=1;
foreach ($months as $month)
{
	if ($now_month == $i)
		echo "<option value=\"$i\" selected>$month</option>";	
	else
		echo "<option value=\"$i\">$month</option>";
	$i++;
}
?>
	</select>
	<select name="fday">
<?
//select this day
if (!$fday)
{
	$now_day = date("j");
	$days_in_month = date ("t");
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
else
{
	$now_day = $fday;
	//need to get this month
	$days_in_month = date ("t", strtotime("$fday $months[$fmonth] $fyear"));
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
?>
	</select>
	<select name="fyear">
<?
//select this year
if (!$fyear)
	$now_year = date("Y");
else
	$now_year = $fyear;
for ($i=2003;$i<=2014;$i++)
{
	if ($now_year == $i)
		echo "<option value=\"$i\" selected>$i</option>";	
	else
		echo "<option value=\"$i\">$i</option>";
}
?>
	</select>
	</td>
</tr>
<tr>
	<td valign="top">End Date</td>
	<td valign="top">
	<select name="tmonth">
<?
//set the month
if (!$tmonth)
	$now_month = 12;
else
	$now_month = $tmonth;
$months = array(1 => 'Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$i=1;
foreach ($months as $month)
{
	if ($now_month == $i)
		echo "<option value=\"$i\" selected>$month</option>";	
	else
		echo "<option value=\"$i\">$month</option>";
	$i++;
}
?>
	</select>
	<select name="tday">
<?
//select this day
if (!$tday)
{
	for ($i=1;$i<=31;$i++)
	{
		if ($i == 31)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
else
{
	$now_day = $tday;
	$days_in_month = date ("t");
	for ($i=1;$i<=$days_in_month;$i++)
	{
		if ($now_day == $i)
			echo "<option value=\"$i\" selected>$i</option>";	
		else
			echo "<option value=\"$i\">$i</option>";
	}
}
?>
	</select>
	<select name="tyear">
<?
//select this year
if (!$tyear)
	$now_year = 2014;
else
	$now_year = $tyear;
for ($i=2003;$i<=2014;$i++)
{
	if ($now_year == $i)
		echo "<option value=\"$i\" selected>$i</option>";	
	else
		echo "<option value=\"$i\">$i</option>";
}
?>
	</select>
	</td>
</tr>
<tr>
	<td valign="top">Description</td>
	<td valign="top">
		<textarea rows="3" cols="20" name="description"><?=$description?></textarea>
	</td>
</tr>
</table>
<!--
<p><u>Optional Information</u>
<p>
Enter any additional information that you would like. These fields are optional.</p>
<table width="500" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="white"  bordercolor="white">
<tr>
    <td>Contact</td>
    <td>
		<input maxlength="40" size="22" name="optcontact" value="<?=$optcontact?>">
	</td>
    <td>Site URL</td>
    <td>
		<input maxlength="255" size="20" name="opturl" value="<?=$opturl?>">
	</td>
</tr>
<tr>
    <td>Email</td>
    <td>
		<input maxlength="40" size=:22" name="optemail" value="<?=$optemail?>">
	</td>
    <td>Phone</td>
    <td>
		<input maxLength="40" size="22" name="optphone" value="<?=$optphone?>">
	</td>
</tr>
<tr>
    <td valign="top">Ad Copy or Notes</td>
    <td colspan="3">
		<textarea name="optnotes" rows="10" cols="40"><?=$optnotes?></textarea>
	</td>
</tr>
</table>
-->

<br><br>
<center>
<input type="hidden" name="cID" value="<?=$cID?>">
<input type="submit" name="submit" value="Save Changes"  >
</center></form><br>


<?

require "footer.php";

?>